package com.tan.androidtest.socket;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.tan.androidtest.R;
import com.tan.androidtest.base.BaseActivity;
import com.tan.androidtest.util.ToastUtil;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * 测试简单的Socket通讯
 */

public class TestSimpleSocketClientActivity extends BaseActivity implements View.OnClickListener {

    private TextView mTvContent;
    private EditText mEtContent;
    private TextView mTvSend;

    private Socket mSocket;
    private BufferedReader mBufferedReader;//用于读取服务器发送过来的数据的
    private String mResponseMessage;//
    private PrintWriter mPrintWriter;//用于向服务器写数据的
    private boolean closed = false;

    private static final int RECEIVE_MESSAGE = 0x100;//接收到服务器消息

    @Override
    public int initLayoutId() {
        return R.layout.activity_test_simple_socket_layout;
    }

    @Override
    public void initTitle(TextView tv_bar_left, TextView tv_bar_title, TextView tv_bar_right) {
        tv_bar_title.setText("测试简单的Socket通讯");
    }

    @Override
    public void initView() {
        mTvContent = findViewById(R.id.tv_content);
        mEtContent = findViewById(R.id.et_content);
        mTvSend = findViewById(R.id.tv_send);
        mTvSend.setOnClickListener(this);
    }

    @Override
    public void initData() {
        connectSocket();
    }

    /**
     * 建立连接
     * 新开启一个线程
     * 并使用while死循环来获取服务器传过来的数据
     */
    private void connectSocket() {
        new Thread() {

            @Override
            public void run() {
                try {
                    mSocket = new Socket("192.168.102.117", 8555);
                    mBufferedReader = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
                    mPrintWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(mSocket.getOutputStream())), true);

                    //循环读取
                    while (!closed) {
                        mResponseMessage = mBufferedReader.readLine();
                        mMainHandler.sendEmptyMessage(RECEIVE_MESSAGE);
                        Thread.sleep(100);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_send: {
                sendMessage();
                break;
            }
        }
    }


    /**
     * 向服务器发送数据
     */
    private void sendMessage() {
        String content = mEtContent.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            ToastUtil.showToast(getApplicationContext(), "请输入文字");
            return;
        }

        if (mSocket != null && mSocket.isConnected()) {
            if (!mSocket.isOutputShutdown()) {
                mPrintWriter.println(content);
            }
        }
    }

    private Handler mMainHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case RECEIVE_MESSAGE: {
                    mTvContent.append(mResponseMessage + "\n");
                    break;
                }
            }

        }
    };


}
